/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Training;

import Course.CourseBean;
import User.UserBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author mishra
 */
public class TrainingList extends HttpServlet {

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            Vector<Object> list=Training.Query.view();
           if(!list.isEmpty())
           {
               Iterator it=list.iterator();
               while(it.hasNext())
               {
                  TrainingBean tb=(TrainingBean)it.next();
                  CourseBean cb=tb.getCourseBean();
                   UserBean ub=tb.getUserBean();
                  out.println("<tr id='"+tb.getTrainingId()+"'>");
                  
                  out.println("<td>");
                  out.println("<b>Company Name:</b>&nbsp"+tb.getCompanyName()+"<br><b>Email:</b>&nbsp"+tb.getCompanyemail()+"<br><b>Phone No:</b>&nbsp"+tb.getCompanyPhone()+"<br><b>Address:</b>&nbsp"+tb.getCompanyAddress());
                  out.println("</td>");
                  
                  out.println("<td>");
                  out.println("<b>Coordinator:</b>&nbsp"+tb.getCoordinator()+"<br><b>Training Title:</b>&nbsp"+tb.getTraningTitle()+"<br><b>Training Location:</b>&nbsp"+tb.getTrainingLocation()+"<br><b>Fees:</b>&nbsp"+tb.getTrainignCost()+"<br><b>No of Seats:</b>&nbsp"+tb.getNoOfSeats());
                  out.println("</td>");
                  
                  String strDate="", endDate="";
                  String Enddate[]={};
                  String StartDate="", EndDate="";
                   
                   strDate = tb.getStartTrainingDate().toString();
                   String strartd[] = strDate.split("-");
                   StartDate= strartd[1]+"/"+strartd[2]+"/"+strartd[0];
                   
                   endDate = tb.getEndTraniningDate().toString(); 
                   Enddate = endDate.split("-");
                   EndDate = Enddate[1]+"/"+Enddate[2]+"/"+Enddate[0];
                  
                  out.println("<td>");
                  out.println("<b>Starting Date:</b>&nbsp"+tb.getStartTrainingDate()+"<br><b>End Date:</b>&nbsp"+tb.getEndTraniningDate()+"<br><b>Timing</b>&nbsp"+tb.getTimeFrom()+"-"+tb.getTimeTo());
                  out.println("</td>");
                  
                  out.println("<td>");
                  out.println("<b>Course Name:</b>&nbsp"+cb.getCourseName()+"<br><b>Stream:</b>&nbsp"+cb.getStream());
                  out.println("</td>");
                  
                  out.println("<td>");
                  out.println("<b>Description:</b>&nbsp"+tb.getDescription());
                  out.println("</td>");
                  
                  
                  
                  out.println("<td style='text-align:center;' id='studed'>");
                  out.println("<a href='EditTraining.jsp?tid="+tb.getTrainingId()+"&comname="+tb.getCompanyName()+"&comadd="+tb.getCompanyAddress()+"&comphone="+tb.getCompanyPhone()+"&comemail="+tb.getCompanyemail()+"&startdate="+StartDate+"&enddate="+EndDate+"&ttitle="+tb.getTraningTitle()+"&timefrom="+tb.getTimeFrom()+"&timeto="+tb.getTimeTo()+"&coordinate="+tb.getCoordinator()+"&tlocation="+tb.getTrainingLocation()+"&tcost="+tb.getTrainignCost()+"&desc="+tb.getDescription()+"&userid="+tb.getUserId()+"&entrydate="+tb.getEntryDate()+"&courseid="+cb.getCourseId()+"&coursename="+cb.getCourseName()+"&stream="+cb.getStream()+"&noseats="+tb.getNoOfSeats()+"'><button class='btn btn-info' style='min-width: 67px;' onclick='removenull()'>Edit</button></a>");
                  out.println("<br><br><a href='#'><button class='btn btn-danger' onclick='deleteTrainingConfirm("+tb.getTrainingId() +")'>Delete</button></a>");
                  out.println("</td>");
                  out.println("</tr>");
               }
           }
           else
           {
               out.println("<tr>");
               out.println("<td colspan='3'>");
               out.println("<b>No Record Found</b>");
               out.println("</td>");
               out.println("</tr>");
           }
        
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(TrainingList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(TrainingList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
